local ligong = fk.CreateSkill {

  name = "joy__ligong",

  tags = { Skill.Wake, },

}



ligong:addEffect(fk.EventPhaseStart, {
  name = "joy__ligong",
  frequency = Skill.Wake,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(ligong.name) and
      player.phase == Player.Start and
      player:usedSkillTimes(ligong.name, Player.HistoryGame) == 0
  end,
  can_wake = function(self, event, target, player, data)
    return player:hasSkill("huishu", true) and
    (player:getMark("huishu1") > 1 or player:getMark("huishu2") > 3 or player:getMark("huishu3") > 2)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, 1)
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = ligong.name
    })
    room:handleAddLoseSkills(player, "-yishu", nil)
    local generals = {}
    for _, general in pairs(Fk.generals) do
      if general.package.extensionName == "joy" and general.gender == General.Female and general.kingdom == "wu" and general.name ~= "joy__quanhuijie" then
        table.insert(generals, general.name)
      end
    end
    local choices = {}
    if #generals > 0 then 
    generals = table.random(generals, 4)
    local skills = {}
    for _, general_name in ipairs(generals) do
      local general = Fk.generals[general_name]
      local g_skills = {}
      for _, skill in ipairs(general.skills) do
        if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest}, skill.frequency) or skill.lordSkill) and
        (#skill.attachedKingdom == 0 or (table.contains(skill.attachedKingdom, "wu") and player.kingdom == "wu")) then
          table.insertIfNeed(g_skills, skill.name)
        end
      end
      for _, s_name in ipairs(general.other_skills) do
        local skill = Fk.skills[s_name]
        if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest}, skill.frequency) or skill.lordSkill) and
        (#skill.attachedKingdom == 0 or (table.contains(skill.attachedKingdom, "wu") and player.kingdom == "wu")) then
          table.insertIfNeed(g_skills, skill.name)
        end
      end
      table.insertIfNeed(skills, g_skills)
    end
    local result = player.room:askForCustomDialog(player, ligong.name,
    "packages/tenyear/qml/ChooseGeneralSkillsBox.qml", {
      generals, skills, 1, 2, "#ligong-choice", true
    })
    if result ~= "" then
      choices = json.decode(result)
    end
    end
    if #choices == 0 then
      player:drawCards(3, ligong.name)
    else
      room:handleAddLoseSkills(player, "-huishu|"..table.concat(choices, "|"), nil)
    end
  end,
})

return ligong